package com.example.myaccount.component;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

import com.example.myaccount.R;
import com.example.myaccount.activity.AddFlowActivity;
import com.example.myaccount.activity.MainActivity;
import com.example.myaccount.constant.MyConst;
import com.example.myaccount.util.MathUtil;
import com.example.myaccount.util.ResourceUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class FlowDialog extends Dialog implements View.OnClickListener {

    private PriorityListener listener;

    private double amount;
    private String amountText = new String();
    private ArrayList<String> numbers;

    private TextView mTv0,mTv1,mTv2,mTv3,mTv4,mTv5,mTv6,mTv7,mTv8,mTv9,mTvDate,mTvMinus,mTvPlus,mTvEquals,mTvDot;
    private EditText mEtAnnotation,mEtAmount;
    private LinearLayout mLlDelete;

    /**
     * 自定义Dialog监听器
     */
    public interface PriorityListener {
        /**
         * 回调函数，用于在Dialog的监听事件触发后刷新Activity的UI显示
         */
        public void refreshPriorityUI(Double amount);
    }

    public FlowDialog(Context context,PriorityListener listener) {
        super(context);
        this.listener = listener;
    }

    public FlowDialog(Context context, int themeResId,PriorityListener listener) {
        super(context, themeResId);
        this.listener = listener;
    }

    protected FlowDialog(Context context, boolean cancelable, OnCancelListener cancelListener,PriorityListener listener) {
        super(context, cancelable, cancelListener);
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_flow);

        //设置对话框参数
        setDialog();

        //初始化控件
        initView();
//        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_flow, null);
//        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
////        params.width = getResources().getDisplayMetrics().widthPixels - DensityUtil.dp2px(this, 16f);
////        params.bottomMargin = DensityUtil.dp2px(this, 8f);
//        contentView.setLayoutParams(params);
    }


    private void setDialog() {

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = getWindow();
        lp.copyFrom(window.getAttributes());
        //设置长宽
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置起始位置
//        lp.x  = 100;
//        lp.y = 100;
        //设置对话框置于底部
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
        //设置对话框出现动画
        window.setWindowAnimations(R.style.BottomDialog_Animation);

    }

    private void initView() {

        WindowManager wm = (WindowManager) AddFlowActivity.getActivity().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        ViewGroup.LayoutParams lp;

        mTv0 = findViewById(R.id.tv_0);
        lp = mTv0.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv0.setLayoutParams(lp);

        mTv1 = findViewById(R.id.tv_1);
        lp = mTv1.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv1.setLayoutParams(lp);

        mTv2 = findViewById(R.id.tv_2);
        lp = mTv2.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv2.setLayoutParams(lp);

        mTv3 = findViewById(R.id.tv_3);
        lp = mTv3.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv3.setLayoutParams(lp);

        mTv4 = findViewById(R.id.tv_4);
        lp = mTv4.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv4.setLayoutParams(lp);

        mTv5 = findViewById(R.id.tv_5);
        lp = mTv5.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv5.setLayoutParams(lp);

        mTv6 = findViewById(R.id.tv_6);
        lp = mTv6.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv6.setLayoutParams(lp);

        mTv7 = findViewById(R.id.tv_7);
        lp = mTv7.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv7.setLayoutParams(lp);

        mTv8 = findViewById(R.id.tv_8);
        lp = mTv8.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv8.setLayoutParams(lp);

        mTv9 = findViewById(R.id.tv_9);
        lp = mTv9.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTv9.setLayoutParams(lp);

        mTvPlus = findViewById(R.id.tv_plus);
        lp = mTvPlus.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTvPlus.setLayoutParams(lp);

        mTvMinus = findViewById(R.id.tv_minus);
        lp = mTvMinus.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTvMinus.setLayoutParams(lp);

        mTvEquals = findViewById(R.id.tv_equals);
        lp = mTvEquals.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTvEquals.setLayoutParams(lp);

        mTvDot = findViewById(R.id.tv_dot);
        lp = mTvDot.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTvDot.setLayoutParams(lp);

        mTvDate = findViewById(R.id.tv_date);
        lp = mTvDate.getLayoutParams();
        lp.width = (int) (width*0.25);
        mTvDate.setLayoutParams(lp);
        DateFormat df = new SimpleDateFormat("yyyy_MM_dd");
        String date = df.format(Calendar.getInstance().getTime());
        mTvDate.setText(date);

        mEtAmount = findViewById(R.id.et_amount);
        lp = mEtAmount.getLayoutParams();
        lp.width = (int) (width*0.25);
        mEtAmount.setLayoutParams(lp);

        mEtAnnotation = findViewById(R.id.et_annotation);
        lp = mEtAnnotation.getLayoutParams();
        lp.width = (int) (width*0.25);
        mEtAnnotation.setLayoutParams(lp);

        mLlDelete = findViewById(R.id.ll_delete);
        lp = mLlDelete.getLayoutParams();
        lp.width = (int) (width*0.25);
        mLlDelete.setLayoutParams(lp);

        mTv0.setOnClickListener(this);
        mTv1.setOnClickListener(this);
        mTv2.setOnClickListener(this);
        mTv3.setOnClickListener(this);
        mTv4.setOnClickListener(this);
        mTv5.setOnClickListener(this);
        mTv6.setOnClickListener(this);
        mTv7.setOnClickListener(this);
        mTv8.setOnClickListener(this);
        mTv9.setOnClickListener(this);

        mTvMinus.setOnClickListener(this);
        mTvPlus.setOnClickListener(this);
        mTvDot.setOnClickListener(this);
        mTvEquals.setOnClickListener(this);
        mLlDelete.setOnClickListener(this);

        mTvDate.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.tv_date:
//                Log.d(MyConst.TAG,"您点击了日期");
                Calendar ca = Calendar.getInstance();
                final int mYear = ca.get(Calendar.YEAR);
                final int mMonth = ca.get(Calendar.MONTH);
                final int mDay = ca.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),R.style.MyDatePickerDialogTheme,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//                                Log.d(MyConst.TAG,year+"_"+month+"_"+dayOfMonth);
                                month=month+1;
                                String date =  year+"_"+month+"_"+dayOfMonth;
                                mTvDate.setText(date);
                            }
                        },mYear,mMonth,mDay);
                datePickerDialog.show();
                break;
            case R.id.tv_0:
                if(!amountText.equals("0")&&!amountText.equals("")){
                    amountText = amountText+"0";
                    mEtAmount.setText(amountText);
                    mEtAmount.setText(amountText);
                }
                break;
            case R.id.tv_1:
                amountText = amountText+"1";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_2:
                amountText = amountText+"2";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_3:
                amountText = amountText+"3";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_4:
                amountText = amountText+"4";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_5:
                amountText = amountText+"5";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_6:
                amountText = amountText+"6";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_7:
                amountText = amountText+"7";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_8:
                amountText = amountText+"8";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_9:
                amountText = amountText+"9";
                mEtAmount.setText(amountText);
                break;
            case R.id.tv_minus:
                if(!amountText.endsWith("-")&&!amountText.endsWith("+")){
                    amountText = amountText+"-";
                    mEtAmount.setText(amountText);
                    mTvEquals.setText("=");
                }
                break;
            case R.id.tv_plus:
                if(!amountText.endsWith("+")&&!amountText.endsWith("-")) {
                    amountText = amountText + "+";
                    mEtAmount.setText(amountText);
                    mTvEquals.setText("=");
                }
                break;
            case R.id.tv_dot:
                if(!amountText.endsWith(".")) {
                    numbers = MathUtil.spiltNumberStr(amountText);
                    if(!MathUtil.isDoubleDot(numbers.get(numbers.size()-1))){
                        amountText = amountText + ".";
                        mEtAmount.setText(amountText);
                    }
                }
                break;
            case R.id.ll_delete:
                if(amountText.length()!=0){
                    amountText = amountText.substring(0, amountText.length()-1);
                    mEtAmount.setText(amountText);
                }
                break;
            case R.id.tv_equals:
                if(mTvEquals.getText().toString().equals("=")){
                    numbers = MathUtil.spiltNumberStr(amountText);
                    amount = MathUtil.calculateNumbers(numbers);
                    amountText = String.valueOf(amount);
                    mEtAmount.setText(amountText);
                    mTvEquals.setText(ResourceUtil.getResId("add_finish",R.string.class));
                }else{
                    numbers = MathUtil.spiltNumberStr(amountText);
                    amount = MathUtil.calculateNumbers(numbers);
                    if(amount==0){
                        Toast.makeText(getContext(), ResourceUtil.getResId("tips_zero",R.string.class),Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Log.d(MyConst.TAG,""+amount);
                    listener.refreshPriorityUI(amount);
                    cancel();
                }
                break;
        }
    }

    public String getAnnotation(){
        return mEtAnnotation.getText().toString();
    }
    public String getDate(){
        return mTvDate.getText().toString();
    }
}
